#include "global_val.h"
#include "dev_related.h"
#include "sql.h"
#include "websocketserver.h"
#include "cJSON.h"

static char *sensor_table[]  = {NULL, NULL, "Psst_Table", NULL, NULL, NULL, "Light_Table", "Temperature_Table", "Humidity_Table", "Door_Table", "Fan_Table", "Beep_Table", "Smoke_Table", "Calorifier_Table", "Curtain_Table"};


void *Get_Dev_Status_list(void *arg)
{
    int res=0,i,count=0;
	int buf[32][2];
	int col[2]={0,2};
	cJSON *js_root,*js_dev_arry,*js_item;//json根节点、数组、对象
	char *json_out;
	char *json_root_name = "dev_list";
	char *js_item_field[]={"dev_name", "dev_status", "dev_function"};
	char *room_name_table[]={"卧室", "客厅", "厨房", "浴室"};
	char *room_function_table[]={"灯光-窗帘", "灯光-温湿度-光强-门", "灯光-烟雾-风扇-蜂鸣器", "灯光-热水器"};

	//从数据库中获取对应的数据列表
	while(1)
    {
        //等待信号
        poperation(sem_send_dev_online_list, g_sem_id);
        res = sql_get_table_msg_int("Device", col, 2, buf);
        printf("res = %d\n", res);
        if(res!=-1)
        {
            //创建根节点对象和数组对象
            js_root     = cJSON_CreateObject();
            js_dev_arry = cJSON_CreateArray();
            for(i=0; i<res; ++i)
            {
                js_item = cJSON_CreateObject();
                //printf("buf[%d][0]=%d\n", i, buf[i][0]);
                cJSON_AddItemToObject(js_item, js_item_field[0], cJSON_CreateString(room_name_table[buf[i][0]-2]));
                if(buf[i][1]==ZB_DEV_ONLINE)
                {
                    cJSON_AddTrueToObject(js_item, js_item_field[1]);
                }
                else
                {
                    cJSON_AddFalseToObject(js_item, js_item_field[1]);
                }
                cJSON_AddItemToObject(js_item, js_item_field[2], cJSON_CreateString(room_function_table[buf[i][0]-2]));
                cJSON_AddItemToArray(js_dev_arry, js_item);
            }
            cJSON_AddItemToObject(js_root, json_root_name, js_dev_arry);
            json_out = cJSON_Print(js_root);
            server_msg_str_update(json_out);
            cJSON_Delete(js_root);
            free(json_out);
        }
    }
}

void *Get_Dev_Cfg_list(void *arg)
{
    int res=0,i,j,k,l,count=0;
	char buf[32][10][256];
	cJSON *js_root,*js_dev_arry,*js_item,*js_child_item,*js_child_array;//json根节点、数组、对象
	char *json_out;
	char *json_root_name = "dev_cfg_list";
	char *js_item_field[]={"dev_mac", "dev_name", "dev_type", "dev_region", "dev_status"};
	char *js_item_child_field[]={"sensor_type", "sensor_name"};
	int col[]={0,1,2,3,4};//构造列数组
	char col_type[]={SQL_FIEL_TYPE_STR, SQL_FIEL_TYPE_STR, SQL_FIEL_TYPE_INT, SQL_FIEL_TYPE_STR, SQL_FIEL_TYPE_INT};
	int result;
	char child_sensor_name[128];

//#define SMART_HOME_DEBUG
	
	//从数据库中获取对应的数据列表
	while(1)
    {
        //等待信号
        poperation(sem_send_dev_cfg_list, g_sem_id);
        res = sql_get_table_nfield("DeviceCfg", col, col_type, 5, buf);
#ifdef SMART_HOME_DEBUG
		printf("buf[0][0][0]=%s\n", buf[0][0]);
        printf("res = %d\n", res);
#endif
        if(res!=-1)
        {
            //创建根节点对象和数组对象
            js_root     = cJSON_CreateObject();
            js_dev_arry = cJSON_CreateArray();
            for(i=0; i<res; ++i)
            {
                js_item        = cJSON_CreateObject();
#ifdef SMART_HOME_DEBUG
                printf("buf[%d][0]=%d\n", i, buf[i][0]);
#endif
				for(j=0; j<5; ++j)
				{
					if(j==2||j==4)
					{
						k=0;
						result=0;
						while(buf[i][j][k]!='\0')
						{
							result = (result*10)+(buf[i][j][k]-'0');
							++k;
						}
						
						//printf("result = %d\n", result);
						
						if(j==2)
						{
							cJSON_AddItemToObject(js_item, js_item_field[j], cJSON_CreateNumber(result));
							
							js_child_array = cJSON_CreateArray();
							for(k=0; k<32; ++k)
							{
								if(result & (1<<k))
								{
									js_child_item  = cJSON_CreateObject();
									cJSON_AddItemToObject(js_child_item, js_item_child_field[0], cJSON_CreateNumber(k));
									//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
									sql_query_strfield_by_strfield(sensor_table[k], js_item_child_field[1], child_sensor_name, js_item_field[0], buf[i][0]);
									//printf("child_sensor_name:%s\n", child_sensor_name);
									cJSON_AddItemToObject(js_child_item, js_item_child_field[1], cJSON_CreateString(child_sensor_name));
									cJSON_AddItemToArray(js_child_array, js_child_item);
								}
							}
							cJSON_AddItemToObject(js_item, "sensor_info", js_child_array);
						}
						else
						{
							//printf("j=4:%s\n", buf[i][j]);
							if((int)result==ZB_DEV_ONLINE)
							{
								//printf("j=%d result:%d\n", j, ZB_DEV_ONLINE);
								//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
								cJSON_AddTrueToObject(js_item, js_item_field[j]);
							}
							else
							{
								//printf("j=%d result:%d\n", j, ZB_DEV_OFFLINE);
								//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
								cJSON_AddFalseToObject(js_item, js_item_field[j]);
							}
						}
						//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
					}
					else 
					{
						cJSON_AddItemToObject(js_item, js_item_field[j], cJSON_CreateString(buf[i][j]));
					}
					//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
				}
				//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
                cJSON_AddItemToArray(js_dev_arry, js_item);
				//printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
            }
			printf("%s-%s:%d\n", __FILE__, __FUNCTION__, __LINE__);
            cJSON_AddItemToObject(js_root, json_root_name, js_dev_arry);
            json_out = cJSON_Print(js_root);
            server_msg_str_update(json_out);
            cJSON_Delete(js_root);
            free(json_out);
        }
    }
}

/*
 {"root":{{"dev_mac":"", "":"","child":{}},{}}}
*/